package edu.guigu.todolist.todo.view;

import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import edu.guigu.todolist.commons.LogUtils;
import edu.guigu.todolist.databinding.LayoutItemTodoBinding;
import edu.guigu.todolist.todo.model.info.TodoInfo;

public class TodoAdapter extends RecyclerView.Adapter<TodoAdapter.ViewHolder> {

    private LayoutItemTodoBinding binding;

    private List<TodoInfo> todoInfoList = new ArrayList<>();

    private OnItemChildLongClickListener onItemChildLongClickListener;

    public TodoAdapter(OnItemChildLongClickListener onItemChildLongClickListener) {
        this.onItemChildLongClickListener = onItemChildLongClickListener;
    }

    public List<TodoInfo> getTodoInfoList() {
        return todoInfoList;
    }

    public void setTodoInfoList(List<TodoInfo> todoInfoList) {
        this.todoInfoList = null;
        this.todoInfoList = todoInfoList;
    }

    @NonNull
    @Override
    public TodoAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item_todo,parent,false);
        binding = LayoutItemTodoBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        ViewHolder viewHolder = new ViewHolder(binding);
        LogUtils.d("onCreateViewHolder " + viewHolder.hashCode());
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull TodoAdapter.ViewHolder holder, int position) {
        holder.rb_select.setChecked(false);
        holder.tv_title.setText(todoInfoList.get(position).getTitle());
    }

    @Override
    public int getItemCount() {
        return todoInfoList.size();
    }


    public interface OnItemChildLongClickListener {
        /**
         * 子组件点击事件
         * @param view
         * @param position
         * @param type 子组件类型，用于判断是哪个子组件
         */
        void onItemChildLongClick(View view, int position, String type);
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        public TextView tv_title;
        public RadioButton rb_select;
        public CardView cardView;

        public ViewHolder(@NonNull LayoutItemTodoBinding view) {
//            super(view);
            super(view.getRoot());
            // tv_title = view.findViewById(R.id.tv_title);
            // rb_select = view.findViewById(R.id.rb_select);
            // cardView = view.findViewById(R.id.cardView);
            tv_title = view.tvTitle;
            rb_select = view.rbSelect;
            cardView = view.cardView;
            cardView.setOnClickListener(this);
            cardView.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View v) {
            rb_select.setChecked(!rb_select.isChecked());
            if (rb_select.isChecked()) {
                // 启用删除线
                tv_title.setPaintFlags(tv_title.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            } else {
                tv_title.setPaintFlags(tv_title.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);

            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (onItemChildLongClickListener != null) {
                onItemChildLongClickListener.onItemChildLongClick(v, getAdapterPosition(), "chil_1");
            }
            return true;
        }
    }

}
